<?php

require_once '../classes/includeAll.php';
$title="Users";
$head ='<script src="../javascript/pagesJavascript/user.js" type="text/javascript"></script>'.
       '<link rel="stylesheet" type="text/css" href="../design/css/users.css">';


if(!$isAdmin) 
{
  header("Location: user.php");
  exit();
}
require '../resources/getConnection.php';
$userClass = new users($connection);
$error = false;
//auswertung
if(isset($_POST["submit"]))
{
   $name = filter_input(INPUT_POST, "name");
  $pw1=  filter_input(INPUT_POST, "password1");
  $pw2=filter_input(INPUT_POST,"password2");
  $rights = filter_input(INPUT_POST, "rights",FILTER_VALIDATE_INT);
  $email = filter_input(INPUT_POST, "email",FILTER_VALIDATE_EMAIL);
  
  $argument = array();
  array_push($argument, null);
  
  if($name != null && $name != false)
  {
    $oldUser=$userClass->GetUserByName($name);
    if($oldUser == false)
      array_push($argument, $name);
  }
  
  if($pw1 != null && $pw1 != false && $pw2 != null && $pw2 != false && $pw1 == $pw2)
  {
    array_push($argument, EncryptPw($pw1));
  }
  
  if($rights != null && $rights != false && $rights < 3 && $rights >= 1)
  {
    $rights--;
    array_push($argument, $rights);
  }
  
  if($email != null && $email != false)
  {    
    array_push($argument, $email);
  }
  else
    array_pusth($argument,"");
  
  if(count($argument)!= 5)
  {
    $error= true;
  }
  else
  {
  $userClass->AddUser($argument);
  }
}

$userList = $userClass->GetUserTable();
$connection->disconnectDB();

require '../menu/head.php';
echo '<h1>Alle Benutzer</h1>';
if($error)
  echo'<p id="error">Hinzuf&uuml;gen des Users fehlgeschlagen!</p>';

echo '<table id="usersList">'
. '<thead><tr><th>Name</th><th>Rechte</th><th>E-mail</th><th/></tr></thead><tbody>';

foreach ($userList as $user) {
  echo '<tr>';
  echo '<td><a href ="user.php?id='.$user[0].'">'.$user[1].'</a></td>';
  echo '<td><a href ="user.php?id='.$user[0].'">'.$user[2].'</a></td>';
  echo '<td><a href ="user.php?id='.$user[0].'">'.$user[3]."</a></td>";
  echo '<td onclick="deleteUser(this,'.$user[0].')"><img src="../design/pictures/delete.png" width="20" height="20" alt="X"/></td>';
  echo '</tr>';
}
echo '</tbody></table>';

echo '<p><input type="button" value="neuer User" name="new" onclick="addNewUser(this.parentNode)"/></p>';
require '../menu/footer.php';
?>
